﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//-------------------------------------------------------------------------------------

using System.Data;

namespace DotNet.Business
{
    using DotNet.BaseManager;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// BaseUserRoleManager
    /// 职员角色权限
    ///
    /// 修改纪录
    ///
    ///     2008.05.24 版本：1.0 DotNet 创建主键。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2008.04.16</date>
    /// </author>
    /// </summary>
    public partial class BaseUserRoleManager : BaseManager //, IBaseUserRoleManager
    {
        #region public string GetUserFullName(string id) 获取职员名称

        /// <summary>
        /// 获取职员名称
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>主键</returns>
        public string GetUserFullName(string id)
        {
            string userId = this.GetProperty(id, BaseUserRoleTable.FieldUserId);
            return DbLogic.GetProperty(DbHelper, BaseStaffTable.TableName, BaseStaffTable.FieldId, userId, BaseStaffTable.FieldRealName);
        }

        #endregion public string GetUserFullName(string id) 获取职员名称

        #region public string GetRoleName(string id) 获取角色名称

        /// <summary>
        /// 获取角色名称
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>主键</returns>
        public string GetRoleName(string id)
        {
            string roleId = this.GetProperty(id, BaseUserRoleTable.FieldRoleId);
            return DbLogic.GetProperty(DbHelper, BaseRoleTable.TableName, BaseRoleTable.FieldId, roleId, BaseRoleTable.FieldRealName);
        }

        #endregion public string GetRoleName(string id) 获取角色名称

        public bool UserInRole(string userId, string roleCode)
        {
            bool returnValue = false;
            if (string.IsNullOrEmpty(roleCode))
            {
                return false;
            }
            BaseRoleManager roleManager = new BaseRoleManager(this.DbHelper, this.UserInfo);
            string roleId = roleManager.GetID(BaseRoleTable.FieldDeletionStateCode, 0, BaseRoleTable.FieldCode, roleCode);
            if (string.IsNullOrEmpty(roleId))
            {
                return false;
            }
            string[] roleIds = GetAllRoleIds(userId);
            returnValue = BaseBusinessLogic.Exists(roleIds, roleId);
            return returnValue;
        }

        #region public string[] GetRoleIds(string userId) 获取职员的角色主键数组

        /// <summary>
        /// 获取职员的角色主键数组
        /// </summary>
        /// <param name="userId">职员代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetRoleIds(string userId)
        {
            // 被删除的角色不应该显示出来
            string sqlQuery = " SELECT ROLEID  "
                            + "   FROM Base_UserRole "
                            + "  WHERE (USERID = " + userId + ") "
                            + "    AND (ROLEID IN (SELECT ID FROM BASE_ROLE WHERE (DELETIONSTATECODE = 0))) AND (DELETIONSTATECODE = 0) ";
            DataTable dataTable = DbHelper.Fill(sqlQuery);
            return BaseBusinessLogic.FieldToArray(dataTable, BaseUserRoleTable.FieldRoleId);
        }

        #endregion public string[] GetRoleIds(string userId) 获取职员的角色主键数组

        #region public string[] GetRoleIds(string userId) 获取职员的角色主键数组

        /// <summary>
        /// 获取职员的角色主键数组
        /// </summary>
        /// <param name="userId">职员代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetAllRoleIds(string userId)
        {
            // 被删除的角色不应该显示出来
            string sqlQuery = " SELECT ROLEID FROM BASE_USER WHERE (Id = " + userId + ") AND (DeletionStateCode = 0) AND (Enabled = 1) "
                            + " UNION SELECT ROLEID FROM BASE_USERROLE WHERE (UserId = " + userId + ") AND (RoleId IN (SELECT Id FROM Base_Role WHERE (DeletionStateCode = 0))) AND (DeletionStateCode = 0) ";
            DataTable dataTable = DbHelper.Fill(sqlQuery);
            return BaseBusinessLogic.FieldToArray(dataTable, BaseUserRoleTable.FieldRoleId);
        }

        #endregion public string[] GetRoleIds(string userId) 获取职员的角色主键数组

        #region public string[] GetUserIds(string roleId) 获取职员的角色主键数组

        /// <summary>
        /// 获取职员的角色主键数组
        /// </summary>
        /// <param name="roleId">角色代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetUserIds(string roleId)
        {
            // 需要显示未被删除的用户
            string sqlQuery = " SELECT ID AS USERID FROM BASE_USER WHERE (RoleId = " + roleId + ") AND (DeletionStateCode = 0) AND (Enabled = 1) "
                            + " UNION SELECT USERID FROM BASE_USERROLE WHERE (RoleId = " + roleId + ") AND (UserId IN (SELECT Id FROM Base_User WHERE (DeletionStateCode = 0))) AND (DeletionStateCode = 0) ";
            DataTable dataTable = DbHelper.Fill(sqlQuery);
            return BaseBusinessLogic.FieldToArray(dataTable, BaseUserRoleTable.FieldUserId);
        }

        #endregion public string[] GetUserIds(string roleId) 获取职员的角色主键数组

        public string[] GetUserIds(string[] roleIds)
        {
            string[] userIds = null;
            if (roleIds != null && roleIds.Length > 0)
            {
                // 需要显示未被删除的用户
                string sqlQuery = " SELECT ID AS USERID FROM BASE_USER WHERE (RoleId IN ( " + BaseBusinessLogic.ArrayToList(roleIds) + ")) AND (DeletionStateCode = 0) AND (Enabled = 1) "
                                + " UNION SELECT USERID FROM BASE_USERROLE WHERE (RoleId IN (" + BaseBusinessLogic.ArrayToList(roleIds) + ")) AND (UserId IN (SELECT Id FROM Base_User WHERE (DeletionStateCode = 0))) AND (DeletionStateCode = 0) ";
                DataTable dataTable = DbHelper.Fill(sqlQuery);
                userIds = BaseBusinessLogic.FieldToArray(dataTable, BaseUserRoleTable.FieldUserId);
            }
            return userIds;
        }

        //
        // 加入到角色
        //

        #region public string AddToRole(string userId, string roleId) 为了提高授权的运行速度

        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="roleId">角色主键</param>
        /// <returns>主键</returns>
        public string AddToRole(string userId, string roleId)
        {
            string returnValue = string.Empty;
            BaseUserRoleEntity userRoleEntity = new BaseUserRoleEntity();
            userRoleEntity.UserId = int.Parse(userId);
            userRoleEntity.RoleId = int.Parse(roleId);
            userRoleEntity.Enabled = 1;
            userRoleEntity.DeletionStateCode = 0;
            return this.Add(userRoleEntity);
        }

        #endregion public string AddToRole(string userId, string roleId) 为了提高授权的运行速度

        public int AddToRole(string userId, string[] roleIds)
        {
            int returnValue = 0;
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.AddToRole(userId, roleIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int AddToRole(string[] userIds, string roleId)
        {
            int returnValue = 0;
            for (int i = 0; i < userIds.Length; i++)
            {
                this.AddToRole(userIds[i], roleId);
                returnValue++;
            }
            return returnValue;
        }

        public int AddToRole(string[] userIds, string[] roleIds)
        {
            int returnValue = 0;
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < roleIds.Length; j++)
                {
                    this.AddToRole(userIds[i], roleIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  从角色中删除职员
        //

        #region public int RemoveFormRole(string userId, string roleId) 撤销角色权限

        /// <summary>
        /// 从角色中删除职员
        /// </summary>
        /// <param name="userId">职员主键</param>
        /// <param name="roleId">角色主键</param>
        /// <returns>影响行数</returns>
        public int RemoveFormRole(string userId, string roleId)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = BaseUserRoleTable.FieldUserId;
            values[0] = userId;
            names[1] = BaseUserRoleTable.FieldRoleId;
            values[1] = roleId;
            return this.BatchDelete(names, values);
        }

        #endregion public int RemoveFormRole(string userId, string roleId) 撤销角色权限

        public int RemoveFormRole(string userId, string[] roleIds)
        {
            int returnValue = 0;
            for (int i = 0; i < roleIds.Length; i++)
            {
                returnValue += this.RemoveFormRole(userId, roleIds[i]);
            }
            return returnValue;
        }

        public int RemoveFormRole(string[] userIds, string roleId)
        {
            int returnValue = 0;
            BaseUserManager userManager = new BaseUserManager(this.DbHelper, this.UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                // 这里是不只是从角色用户关系表里移除，还把默认角色也需要移除掉
                returnValue += this.RemoveFormRole(userIds[i], roleId);
                userManager.SetProperty(BaseUserTable.FieldId, userIds[i], BaseUserTable.FieldRoleId, null);
            }
            return returnValue;
        }

        public int RemoveFormRole(string[] userIds, string[] roleIds)
        {
            int returnValue = 0;
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < roleIds.Length; j++)
                {
                    returnValue += this.RemoveFormRole(userIds[i], roleIds[j]);
                }
            }
            return returnValue;
        }
    }
}